/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.internal.core;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;

/**
 * Describes the basic information of an object in the remote VM. The
 * implementation of {@link #equals(Object)} is a little unorthodox, see
 * {@link #equals(Object)} for further details.
 * 
 * @see #equals(Object)
 * @author Brad Reynolds
 */
public class ObjectDescriptor implements IAdaptable {
    private String className;

    private int identityHashCode;

    private String baseName;

    /**
     * 
     */
    public ObjectDescriptor() {
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public void setIdentityHashCode(int identityHashCode) {
        this.identityHashCode = identityHashCode;
    }

    /**
     * @return name of the class being described
     */
    public String getClassName() {
        return className;
    }

    public int getIdentityHashCode() {
        return identityHashCode;
    }

    /**
     * @see java.lang.Object#hashCode()
     * @see #equals(Object)
     */
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((className == null) ? 0 : className.hashCode());
        result = PRIME * result + identityHashCode;
        return result;
    }

    /**
     * The equals implementation is meant to determine equality of the object
     * being described. If an ObjectDescriptor is compared for equality against
     * a BindingEventDescriptor (which extends ObjectDescriptor) and they're
     * both describing the same object the two instances are equal.
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!getClass().isInstance(obj))
            return false;
        final ObjectDescriptor other = (ObjectDescriptor) obj;
        if (className == null) {
            if (other.className != null)
                return false;
        } else if (!className.equals(other.className))
            return false;
        if (identityHashCode != other.identityHashCode)
            return false;
        return true;
    }

    /**
     * @return name portion of the class
     */
    public String getBaseName() {
        if (baseName == null) {
            int pos = className.lastIndexOf('.');

            baseName = (pos > -1) ? className.substring(pos + 1, className.length()) : className;
        }

        return baseName;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
     */
    public Object getAdapter(Class clazz) {
        return Platform.getAdapterManager().getAdapter(this, clazz);
    }
}